//
// Created by Tian on 25-5-2.
//
#include <stdint.h>
#include "led_strip.h"
#include "fan_led.h"

/* Private variables */
static uint8_t led_state;
static led_strip_handle_t led_strip;

/* Public functions */
uint8_t get_led_state(void)
{
    return led_state;
}

#define DEF_SPEED_MAX 1800
#define DEF_SPEED_MIN 0

#define STRIP_LED_PIN_USE 47
#define STRIP_LED_COUNT   16


typedef struct {
    uint8_t r;
    uint8_t g;
    uint8_t b;
} RGBColor;

RGBColor getGradientColor(uint8_t value) {
    RGBColor color = {0}; // 默认关闭(0,0,0)

    if (value == 0) {
        return color; // 0为全关闭
    }

    // 分段处理（每段30个单位）
    if (value <= 30) { // 0-30: 黑→绿渐变
        color.g = (uint8_t)((value / 30.0f) * 255);
    }
    else if (value <= 60) { // 31-60: 绿→蓝渐变
        float ratio = (value - 30) / 30.0f;
        color.g = (uint8_t)(255 * (1.0f - ratio));
        color.b = (uint8_t)(255 * ratio);
    }
    else if (value <= 90) { // 61-90: 蓝→粉渐变
        float ratio = (value - 60) / 30.0f;
        color.b = (uint8_t)(255 * (1.0f - 0.5f * ratio)); // 蓝色减弱
        color.r = (uint8_t)(255 * ratio);                 // 红色增强
        color.g = (uint8_t)(128 * ratio);                 // 少量绿色（粉色含绿）
    }
    else { // 91-100: 粉→红渐变
        float ratio = (value - 90) / 10.0f; // 最后10单位快速变红
        color.r = 255;
        color.g = (uint8_t)(128 * (1.0f - ratio)); // 绿色归零
        color.b = (uint8_t)(128 * (1.0f - ratio)); // 蓝色归零
    }

    return color;
}


void led_set_light(int sp)
{
    if(sp > DEF_SPEED_MAX)
        sp = DEF_SPEED_MAX;
    else if (sp < DEF_SPEED_MIN)
        sp = DEF_SPEED_MIN;

    int cur = (int)((float )( (float )sp / (DEF_SPEED_MAX - DEF_SPEED_MIN)) * 100);
    RGBColor color = getGradientColor(cur);

    for (int i = 0; i < STRIP_LED_COUNT; ++i) {
        led_strip_set_pixel(led_strip, i, color.r, color.g, color.b);
    }
    led_strip_refresh(led_strip);
    led_state = true;

}

void led_on(void)
{
    /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
    led_strip_set_pixel(led_strip, 0, 16, 16, 16);
    /* Refresh the strip to send data */
    led_strip_refresh(led_strip);
    /* Update LED state */
    led_state = true;
}

void led_off(void)
{
    /* Set all LED off to clear all pixels */
    led_strip_clear(led_strip);

    /* Update LED state */
    led_state = false;
}

void fan_led_init(void)
{
    // ESP_LOGI(TAG, "example configured to blink addressable led!");
    /* LED strip initialization with the GPIO and pixels number*/
    led_strip_config_t strip_config = {
            .strip_gpio_num = STRIP_LED_PIN_USE,
            .max_leds = STRIP_LED_COUNT, // at least one LED on board
    };
#if CONFIG_EXAMPLE_BLINK_LED_STRIP_BACKEND_RMT
    led_strip_rmt_config_t rmt_config = {
        .resolution_hz = 10 * 1000 * 1000, // 10MHz
        .flags.with_dma = false,
    };
    ESP_ERROR_CHECK(
        led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
#elif CONFIG_EXAMPLE_BLINK_LED_STRIP_BACKEND_SPI
    led_strip_spi_config_t spi_config = {
        .spi_bus = SPI2_HOST,
        .flags.with_dma = true,
    };
    ESP_ERROR_CHECK(
        led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
#else
#error "unsupported LED strip backend"
#endif
    /* Set all LED off to clear all pixels */
    led_off();
}